var ptCms = {
	iframe:null,
	afterElement:null,
	curElement:null,
	parentCurElement:null,
	layoutHtml:"",
	init:function(){
		
		
		$(document).ready(function (e) {
			$('#project_frame').load(function () { //The function below executes once the iframe has finished loading<---true dat, althoug Is coppypasta from I don't know where
				ptCms.iframe =  $('#project_frame').contents().get(0);
				// $("#project_frame").contents().find("html").html();
				$(ptCms.iframe).live('click',function(e){
					// var tag_name = $(e.target).prop('tagName');
					var attr = $(e.target).attr('ptcms');
					switch(attr){
						case "layout":
							
							ptCms.afterElement = e.target;
							ptCms.curElement = e.target;
							ptCms.parentCurElement = $(ptCms.afterElement).parent();
							ptCms.layoutHtml = $(ptCms.afterElement).parent().html();
							$(ptCms.afterElement).parent().html('php echo $content');
							
							$.post("/api/public/createLayout",{"html":$('#project_frame').contents().find("html").html()})
							.success(function(data, status, headers, config) {
								console.log(data);
								if(data.error){
									showStickyErrorToast(data.message);
								}else{
									
									
									$(ptCms.parentCurElement).html(ptCms.layoutHtml);
									
								}
							}).error(function(data, status, headers, config) {
								if(status=="error") showStickyErrorToast(data.responseText);
								console.log(data);
								console.log(status);
							});
						break;
						default:
							// if( ptCms.afterElement != null ) $(ptCms.afterElement).attr('style','');
							// $(e.target).attr('style','opacity:0.2; background:#fafafa');
							// ptCms.afterElement = e.target;
							// ptCms.curElement = e.target;
						break;
					}
					return false;
				});
				$(ptCms.iframe).keyup(function(e){
					// if(ptCms.curElement != null) $(ptCms.curElement).remove();
					// ptCms.curElement = null;
				});
			});
			
		});
	}

};